/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: Bomb.java 50 2008-05-17 09:24:01Z jens464 $
*/
package albareth.core.entity.item;

import java.util.Iterator;
import java.util.List;

import frosch.util.Range;
import albareth.core.GameEffect;
import albareth.core.action.Action;
import albareth.core.action.Spell;
import albareth.core.buff.BuffHealOverTime;
import albareth.core.entity.ActionItem;
import albareth.core.entity.Item;
import albareth.core.entity.Mobile;
import albareth.frontend.Resources;
import albareth.frontend.Tile;
import albareth.frontend.TileCollection;
import albareth.util.Location;

public class Bomb extends Item
{
    private Range _damage = new Range(8,16);
    private int _timer = 4000;
    private Mobile _activator;

    public Bomb()
    {
        super("Bomb", TileCollection.BOMB);
        setActivatable(true);
        setStackable(true);
    }
    
    public void enchant(int level)
    {
        _damage = new Range(8 + level*5, 16 + level*5);
    }
    
    public boolean onActivate(Mobile src)
    {
        Bomb bomb;
        if (src.containsItem(this)) { //in player inv?
            bomb = (Bomb)src.dropItem(this, 1);
        } else {
            bomb = this; //on ground
        }
        
        bomb.setLockedDown(true);
        bomb.startTimer(_timer);
        bomb._activator = src;
        bomb.emitEffect(GameEffect.entityBound(Resources.EFFECT_FUSE, bomb, _timer));
        src.eventMsg("You activate the bomb.");
        src.emitSound(Resources.SOUND_FUSE_LIGHTER);

        return true;
    }
    
    
    protected void onTimer()
    {
        remove();
        
        GameEffect exploEffect = GameEffect.stationary(
                Resources.EFFECT_EXPLOSION, getLocation(), -1
        );
        emitEffect(exploEffect);
        emitSound(Resources.SOUND_EXPLO_LARGE);

        
        List cellsToExplode = getLocation().getAllNeighborLocations();
        for (Iterator it = cellsToExplode.iterator(); it.hasNext(); )
        {
            Location loc = (Location)it.next();
            loc.inflictDamage(_damage.getRandomValue(), 0, _activator);
        }
    }

    public boolean isStackableWith(Object other)
    {
        if (!(other instanceof Bomb)) {
            return false;
        }
        
        Bomb otherBomb = (Bomb)other;
        return otherBomb._damage.equals(_damage);
    }
}
